package com.rentong.wx.cust.service.impl;

import com.rentong.page.Page;
import com.rentong.wx.cust.dao.CustAddressDao;
import com.rentong.wx.cust.domain.CustAddressEntity;
import com.rentong.wx.cust.service.CustAddressService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class CustAddressServiceImpl implements CustAddressService {

    @Resource
    private CustAddressDao custAddressDao;


    @Transactional(rollbackFor = Exception.class)
    @Override
    public Page getAddress(Long custId, Integer pageNum, Integer pageSize) {
        List<CustAddressEntity> address = custAddressDao.getAddress(custId, pageNum, pageSize);
        Integer count = custAddressDao.getcount(custId, pageNum, pageSize);
        Page page = new Page();
        page.setTotalNum(count);
        page.setResultList(address);
        return page;
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer updateMoRen(Long addressId, Long custId) {
        //先把默认地址去掉
        custAddressDao.removeMoRen(custId);

        //修改一条数据为默认地址
        CustAddressEntity custAddressEntity = new CustAddressEntity();
        custAddressEntity.setAddressId(addressId);
        custAddressEntity.setUpdateTime(new Date());
        custAddressEntity.setMoren(1);
        return custAddressDao.updateByPrimaryKeySelective(custAddressEntity);
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer insertAddress(CustAddressEntity custAddressEntity, Long custId) {
        custAddressEntity.setCustId(custId);
        custAddressEntity.setAddTime(new Date());
        return custAddressDao.insertSelective(custAddressEntity);
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer delAddress(Long addressId) {
        return custAddressDao.deleteByPrimaryKey(addressId);
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer updateAddress(CustAddressEntity custAddressEntity) {
        custAddressEntity.setUpdateTime(new Date());
        return custAddressDao.updateByPrimaryKeySelective(custAddressEntity);
    }

    @Override
    public Object getMorenAddress(Long custId) {
        CustAddressEntity custAddressEntity = new CustAddressEntity();
        custAddressEntity.setCustId(custId);
        custAddressEntity.setMoren(1);
        return custAddressDao.select(custAddressEntity);
    }

}